package com.testtask.rightandabove.controller;

import com.testtask.rightandabove.entity.CD;
import com.testtask.rightandabove.form.UploadForm;
import com.testtask.rightandabove.service.ICDWarehouseService;
import static com.testtask.rightandabove.tools.Command.*;
import com.testtask.rightandabove.tools.Pager;
import static com.testtask.rightandabove.tools.UrlList.*;
import static com.testtask.rightandabove.tools.ViewList.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controller class
 *
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 */
@Controller
public class CDWarehouseController {

    @Autowired
    private ICDWarehouseService cdWarehouseService;

    /**
     * Redirect to DATAIMAGE_URL
     *
     * @return String
     */
    @RequestMapping(value = "/")
    public String home() {
        return "redirect:/" + DATAIMAGE_URL;
    }

    /**
     * Display data from xml file.
     *
     * @param request
     * @return ModelAndView
     */
    @RequestMapping(value = DATAIMAGE_URL)
    public ModelAndView findAll(HttpServletRequest request) {
        Map<String, Object> model = new HashMap<String, Object>();
        String path = request.getSession().getServletContext().getRealPath("/WEB-INF/data");
        String pageStr;
        if (request.getParameter("page") == null) {
            pageStr = "0";
        } else {
            pageStr = request.getParameter("page");
        }
        List<CD> cdList = cdWarehouseService.findAll(path, pageStr);
        model.put("page", pageStr);
        model.put("lastPage", Pager.getLastPage());
        model.put("cdEntities", cdList);
        return new ModelAndView(DATA_IMAGE_VIEW, model);
    }

    /**
     * Display the form for upload the file
     *
     * @return ModelAndView
     */
    @RequestMapping(value = UPLOAD_URL)
    public ModelAndView upload() {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put(DATA_UPLOAD_COMMAND, new UploadForm());
        return new ModelAndView(UPLOAD_VIEW, model);
    }

    /**
     * Downloads the file to the system
     *
     * @param uploadForm
     * @param result
     * @param request
     * @return ModelAndView
     */
    @RequestMapping(value = UPLOAD_URL, method = RequestMethod.POST)
    public ModelAndView upload(@Valid @ModelAttribute(DATA_UPLOAD_COMMAND) UploadForm uploadForm,
            BindingResult result, HttpServletRequest request) {
        if (result.hasErrors()) {
            Map<String, Object> model = new HashMap<String, Object>();
            model.put(DATA_UPLOAD_COMMAND, uploadForm);
            return new ModelAndView(UPLOAD_VIEW, model);
        } else {
            String path = request.getSession().getServletContext().getRealPath("/WEB-INF/data");
            cdWarehouseService.upload(path, uploadForm);
            return new ModelAndView("redirect:/" + DATAIMAGE_URL);
        }
    }

    /**
     * Display reference to save the file of warehouse
     *
     * @return String
     */
    @RequestMapping(value = DOWNLOAD_URL)
    public String download() {
        return DOWNLOAD_VIEW;
    }
}
